package com.scuola.scuoladocente;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class docenteTab extends Activity {
	
	private ListView lv;
	private JSONArray parsedList = null;
	private JSONParser parser = new JSONParser();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_docente_tab);
        
        lv = (ListView)findViewById(R.id.docenteTab_lv);
        ArrayList<String> lvItems = new ArrayList<String>();
        
        try {
        	AsyncTask<Void, Void, String> getMaterieByDocenteIdResponse = new getMaterieByDocenteId().execute();
			parsedList = (JSONArray) parser.parse(getMaterieByDocenteIdResponse.get());
			
			for(int i = 0; i < parsedList.size(); i++ ){
				JSONObject obj = (JSONObject) parser.parse(parsedList.get(i).toString());
				lvItems.add(obj.get("descrizione").toString());
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvItems);
			lv.setAdapter(adapter);
			
			lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					try {
						JSONObject selectedObj = (JSONObject) parser.parse(parsedList.get(arg2).toString());
			           	Intent listaAlunniDocente = new Intent(getApplicationContext(), listaAlunniDocente.class);
			           	listaAlunniDocente.putExtra("idclasse", getIntent().getStringExtra("idclasse"));
			           	listaAlunniDocente.putExtra("idutente", getIntent().getStringExtra("idutente"));
			           	listaAlunniDocente.putExtra("nomeClasse", getIntent().getStringExtra("nomeClasse"));
			           	listaAlunniDocente.putExtra("nomeMateria", selectedObj.get("descrizione").toString());
			           	listaAlunniDocente.putExtra("idMateria", selectedObj.get("id_materia").toString());
			           	startActivityForResult(listaAlunniDocente, 0);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
    private class getMaterieByDocenteId extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("docenteId", getIntent().getStringExtra("idutente"));
				hm.put("classId", getIntent().getStringExtra("idclasse"));
				String strResponse = operations.wsCall("getMaterieByDocenteId", hm).toString();
				return strResponse;
			} catch (Exception e) {
				System.err.println("getMaterieByDocenteId errore (Exception)");
				e.printStackTrace();
				return null;
			}
		}
    }
}
